import groovy.json.*
import org.apache.commons.lang.StringUtils
import redis.clients.jedis.*
import com.alibaba.moco.util.*
import com.alibaba.moco.*
import script.GlobalValues

/**
 * 模拟 滴滴 司机评分
 * Created by wb-lj240895 on 2016/11/1.
 */
def excute(request) {
    def slurper = new JsonSlurper()
    def params = request.getParameterMap();
    Jedis redis = RedisPoolInstance.getRedisClient()
    //欢行异常判断
    def e = redis.hget(GlobalValues.KEY_EXCEPTION,"comment")
    if (!StringUtils.isBlank(e)) {
        def eJson = slurper.parseText(e)
        return eJson
    }
    CommentRequest req = BaseTypeUtil.mapToBean(params, CommentRequest.class) as CommentRequest
    String token = req.getAccess_token()
    System.out.println(token)

    CommentResponse response = new CommentResponse()
    if (token != null /* && redis.exists(token)*/) {
        //redis.del(token)
        //TODO:判断订单是否存在 存在则更新订单信息
        //redis.hset(GlobalValues.KEY_COMMENT,req.order_id,req.comment)


        response.setErrno(0)
        response.setErrmsg("SUCCESS")
        return JsonOutput.toJson(response)
    } else {
        response.setErrno(-1)
        response.setErrmsg("token错误")
        return JsonOutput.toJson(response)
    }
}

public class CommentRequest {
    String client_id    //	string	yes	申请应用时分配的AppKey
    String access_token    //	string	yes	授权后的access token
    String order_id    //	string	yes	订单id
    String level    //	string	yes	司机评分 星级(1-5)
    String comment    //	string	no	司机评价最多40个汉字
    String timestamp    //	int	yes	当前时间戳
    String sign    //	string	yes	签名
}

public class CommentResponse {
    int errno
    String errmsg
    String data
}
